package xian.woniuxy.reghos.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.ResultVO;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.redis.utils.RedisUtils;
import com.ruoyi.common.satoken.utils.LoginHelper;
import com.ruoyi.system.api.RemoteDeptService;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xian.woniuxy.domain.Register;
import xian.woniuxy.customer.dubboService.RemotePatientService;
import xian.woniuxy.reghos.domain.vo.RegisterVo;
import xian.woniuxy.reghos.domain.bo.RegisterBo;
import xian.woniuxy.reghos.service.IRegisterService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 挂号控制器
 * 前端访问路由地址为:/reghos-serv/register
 *
 * @author ataoxz
 * @date 2022-08-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/register")
public class RegisterController extends BaseController {

    private final IRegisterService iRegisterService;

    /**
     * 查询挂号列表
     */
//    @SaCheckPermission("reghos-serv:register:list")
    @GetMapping("/list")
    public TableDataInfo<RegisterVo> list(RegisterBo bo, PageQuery pageQuery) {
        return iRegisterService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出挂号列表
     */
//    @SaCheckPermission("reghos-serv:register:export")
    @Log(title = "挂号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(RegisterBo bo, HttpServletResponse response) {
        List<RegisterVo> list = iRegisterService.queryList(bo);
        ExcelUtil.exportExcel(list, "挂号", RegisterVo.class, response);
    }

    /**
     * 获取挂号详细信息
     *
     * @param registerId 主键
     */
//    @SaCheckPermission("reghos-serv:register:query")
    @GetMapping("/{registerId}")
    public R<RegisterVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long registerId) {
        return R.ok(iRegisterService.queryById(registerId));
    }

    /**
     * 新增挂号
     */
//    @SaCheckPermission("reghos-serv:register:add")
    @Log(title = "挂号", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody RegisterBo bo) {
        return toAjax(iRegisterService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改挂号
     */
//    @SaCheckPermission("reghos-serv:register:edit")
    @Log(title = "挂号", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody RegisterBo bo) {
        return toAjax(iRegisterService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除挂号
     *
     * @param registerIds 主键串
     */
//    @SaCheckPermission("reghos-serv:register:remove")
    @Log(title = "挂号", businessType = BusinessType.DELETE)
    @DeleteMapping("/{registerIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] registerIds) {
        return toAjax(iRegisterService.deleteWithValidByIds(Arrays.asList(registerIds), true) ? 1 : 0);
    }

    @GetMapping("getRegisterByCardId")
    public ResultVO getRegisterByCardId(String cardId) {
        Object cacheObject = RedisUtils.getCacheObject("guahaopatient:" + cardId);
        if (cacheObject == null) {
            return ResultVO.success("no");
        }
        return ResultVO.success().data("register", cacheObject);
    }

    @GetMapping("getRegisterById")
    public ResultVO getRegisterById(String registerId) {
        Object cacheObject = RedisUtils.getCacheObject("guahaoregister:" + registerId);
        if (cacheObject == null) {
            return ResultVO.success("no");
        }
        return ResultVO.success().data("register", cacheObject);
    }

    @GetMapping("queryRegisterListByUser")
    public ResultVO queryRegisterListByUser() {
        Long userId = LoginHelper.getUserId();
        List<Register> registerList = RedisUtils.getCacheList("guahaolist:" + userId);
        return ResultVO.success().data("registerList", registerList);
    }

    @GetMapping("endReg")
    public ResultVO endReg(Long registerId) {
        iRegisterService.endReg(registerId);
        return ResultVO.success();
    }

    @GetMapping("doGuoHao")
    public ResultVO doGuoHao(Long registerId) {
        iRegisterService.doGuoHao(registerId);
        return ResultVO.success();
    }
}
